//
//  DataEnvironment.m
//  FlipBoard
//
//  Created by Kevin Huang on 9/15/10.
//  Copyright 2010 iTotem. All rights reserved.
//

#import "DataEnvironment.h"

@implementation DataEnvironment
static DataEnvironment *sharedInst = nil;

@synthesize rootViewController = _rootViewController;
@synthesize mainViewController = _mainViewController;
@synthesize navController = _navController;
@synthesize collectionNav = _collectionNav;
@synthesize numOfDetailView = _numOfDetailView;
@synthesize showFingerAd = _showFingerAd;
@synthesize showFingerAdInDetailView = _showFingerAdInDetailView;
@synthesize isMainViewPresentingController = _isMainViewPresentingController;

- (void)dealloc 
{
	RELEASE_SAFELY(_mainViewController);
    RELEASE_SAFELY(_navController);
    RELEASE_SAFELY(_collectionNav);
}

+ (id)sharedDataEnvironment 
{
	@synchronized( self ) 
	{
		if ( sharedInst == nil ) 
		{
            sharedInst = [[self alloc] init];
		}
	}
	return sharedInst;
}

- (id)init {
	if ( sharedInst != nil )
    {
        return sharedInst;
	}
    
    self = [super init];
	if (self)
    {
		sharedInst = self;
		[self initData];
	}
	return self;
}

- (void)initData 
{
    _numOfDetailView = 0;
    self.rootViewController = nil;
	self.mainViewController = nil;
    self.navController = nil;
    
    self.showFingerAd = YES;
    self.showFingerAdInDetailView = YES;
    self.isMainViewPresentingController = NO;
}


- (void)pushViewController:(UIViewController *)viewController withAnimate:(BOOL)animate
{
    if (viewController == nil)
    {
        return;
    }
    if (self.collectionNav != nil)
    {
        [self.collectionNav pushViewController:viewController animated:animate];
    }
    else
    {
        [self.navController pushViewController:viewController animated:animate];
    }
}

- (void)pushView:(UIView *)view withAnimate:(BOOL)animate
{
    if (view == nil)
    {
        return;
    }
    UIViewController *viewCon = [[UIViewController alloc] init];
    viewCon.view = view;
    [self pushViewController:viewCon withAnimate:animate];
}

- (void)popViewControllerWithAnimate:(BOOL)animate
{
    if (self.collectionNav != nil)
    {
        [self.collectionNav popViewControllerAnimated:animate];
    }
    else
    {
        [self.navController popViewControllerAnimated:animate];
    }
}

- (UIViewController *)presentingController
{
    UIViewController *viewController = nil;
    UIViewController *currentController = self.navController;
    while (currentController != nil)
    {
        viewController = currentController;
        currentController = currentController.modalViewController;
    }
    return viewController;
}
@end


